/****************************************************************************
 * Copyright (C) 2011
 * by Dimok
 * modified by dj_skual
 * for SaveGame Manager GX 2011
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 ***************************************************************************/
#include "HomebrewPrompt.hpp"
#include "../Themes/CTheme.h"
#include "../Controls/Application.h"

HomebrewPrompt::HomebrewPrompt(const char *name,
							   const char *coder,
							   const char *version,
							   const char *release_date,
							   const char *long_description,
							   GuiImageData * iconImgData,
							   u64 filesize)
	: PromptWindow(name, NULL, tr("Load"), tr("Back"))
{
	whiteBox = Resources::GetImageData("browser.png");
	
	iconImg = new GuiImage(iconImgData);
	iconImg->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	iconImg->SetPosition(40, 36);

	height += 20;
	bgWindowImg->SetScaleY(1.32);
	bgWindowImg->SetPosition(0, 28);
	
	whiteBoxImg = new GuiImage(whiteBox);
	whiteBoxImg->SetScaleX(0.69);
	whiteBoxImg->SetScaleY(0.7);
	whiteBoxImg->SetAlignment(ALIGN_CENTER | ALIGN_TOP);
	whiteBoxImg->SetPosition(0, 51);
	
	titleTxt->SetFontSize(24);
	titleTxt->SetPosition(0, 4);
	titleTxt->SetMaxWidth(360, SCROLL_HORIZONTAL);

	//!convert filesize from u64 to char and put unit of measurement after it
	char filesizeCH[15];
	if (filesize <= 1024.0)
		snprintf(filesizeCH, sizeof(filesizeCH), "%lld B", filesize);
	if (filesize > 1024.0)
		snprintf(filesizeCH, sizeof(filesizeCH), "%0.2f KB", filesize / 1024.0);
	if (filesize > 1048576.0)
		snprintf(filesizeCH, sizeof(filesizeCH), "%0.2f MB", filesize / 1048576.0);

	filesizeTxt = new GuiText(filesizeCH, 16, thColor("r=0 g=0 b=0 a=255 - prompt windows text color"));
	filesizeTxt->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	filesizeTxt->SetPosition(-40, 39);

	release_dateTxt = new GuiText(release_date, 16, thColor("r=0 g=0 b=0 a=255 - prompt windows text color"));
	release_dateTxt->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	release_dateTxt->SetPosition(-40, 62);
	
	coderTxt = new GuiText(fmt(tr( "Coded by: %s" ), coder), 16, thColor("r=0 g=0 b=0 a=255 - prompt windows text color"));
	coderTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	coderTxt->SetPosition(180, 39);
	coderTxt->SetMaxWidth(180, DOTTED);

	versionTxt = new GuiText(fmt(tr( "Version: %s" ), version), 16, thColor("r=0 g=0 b=0 a=255 - prompt windows text color"));
	versionTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	versionTxt->SetPosition(180, 62);
	versionTxt->SetMaxWidth(180, DOTTED);

	const int linestodraw = 7;
	long_descriptionTxt = new GuiLongText(long_description, 20, thColor("r=0 g=0 b=0 a=255 - prompt windows text color"));
	long_descriptionTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	long_descriptionTxt->SetPosition(45, 100);
	long_descriptionTxt->SetLinesToDraw(linestodraw);
	long_descriptionTxt->SetMaxWidth(370);
	long_descriptionTxt->SetBreakLine(true);

	scrollBar = new Scrollbar(212, Scrollbar::LISTMODE);
	scrollBar->SetParent(this);
	scrollBar->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	scrollBar->SetPosition(-31, 83);
	scrollBar->SetScrollSpeed(Settings.ScrollSpeed);
	scrollBar->listChanged.connect(this, &HomebrewPrompt::OnListChange);
	scrollBar->SetEntrieCount(long_descriptionTxt->GetTotalLinesCount()-linestodraw);
	scrollBar->SetPageSize(linestodraw);
	scrollBar->SetRowSize(0);
	scrollBar->SetSelectedItem(0);
	
	Append(whiteBoxImg);
	Append(scrollBar);

	if(strcmp(version, "") != 0) Append(versionTxt);
	if(strcmp(coder, "") != 0) Append(coderTxt);
	if(strcmp(release_date, "") != 0) Append(release_dateTxt);
	if(strcmp(long_description, "") != 0) Append(long_descriptionTxt);
	Append(filesizeTxt);
	Append(iconImg);

	btn1->SetPosition(44, 0);
	btn2->SetPosition(-44, 0);
	
	SetAutoClose(true);
	
	DimBackground(true);
	Application::Instance()->Append(this);
	Application::Instance()->SetUpdateOnly(this);
}

HomebrewPrompt::~HomebrewPrompt()
{
	SetEffect(EFFECT_SLIDE_TOP | EFFECT_SLIDE_OUT, 50);
	
	RemoveAll();
	
	Resources::Remove(whiteBox);
	delete whiteBoxImg;
	
	delete iconImg;
	
	delete coderTxt;
	delete versionTxt;
	delete release_dateTxt;
	delete filesizeTxt;
	delete long_descriptionTxt;
	
	delete scrollBar;
}

void HomebrewPrompt::OnListChange(int selItem, int selIndex)
{
	long_descriptionTxt->SetTextLine(selItem+selIndex);
}
